package game.module.battlereport.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import db.proto.Protoname.Battlename;
import game.db.DataSourceManager;
import game.module.battlereport.bean.PlayerReport;
import lion.common.MyBlob;

public class PlayReportDBDao {

	private static Logger logger = LoggerFactory.getLogger(PlayReportDBDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static PlayReportDBDao instance = new PlayReportDBDao();
	}

	public static PlayReportDBDao getInstance() {
		return SingletonHolder.instance;
	}

	public boolean addPlayReport(PlayerReport playerreport) {
		boolean addRet = false;
		QueryRunner runner = new QueryRunner(dataSource);
		Battlename playsmsg = playerreport.getPlayers();
		byte content1[] = new byte[0];
		if (playsmsg != null) {
			content1 = playsmsg.toByteArray();
		} 
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			MyBlob blob = new MyBlob(content1);
			int ret = runner.update(conn,
					"insert into battle_report(playerId,game_type,date,battlereportId,roomid,playersmsg)" + "values(?,?,?,?,?,?)",
					playerreport.getPlayerId(), playerreport.getGame_type(), playerreport.getDate(),
					playerreport.getBattleperortId(), playerreport.getRoomid(),blob);
		} catch (SQLException e) {
			logger.error("", e);
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return addRet;

	}
}
